﻿Public Partial Class Wfrm_Administracion
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not ViewState("usercontrol") Is Nothing Then
            If ViewState("usercontrol") <> "" Then
                CargaControl(ViewState("usercontrol"), False)

            End If
        End If
    End Sub

    Protected Sub Menu1_MenuItemClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MenuEventArgs) Handles Menu1.MenuItemClick
        CargaControl(e.Item.Value.Replace(".ascx", "") & ".ascx", True)
    End Sub
    Sub CargaControl(ByVal ControlTxt As String, ByVal remove As Boolean)
       
        Try
            If remove Then
                RemoveControls()
                'Try
                '    Dim LastFeaturedProductUserControl As Control
                '    LastFeaturedProductUserControl = LoadControl(ViewState("usercontrol"))
                '    Panel2.Controls.Remove(LastFeaturedProductUserControl)

                '    Dim i As Integer
                '    For i = 0 To Panel2.Controls.Count - 1
                '        Panel2.Controls.Remove(Panel2.Controls(i))
                '        Panel2.UpdateAfterCallBack = True
                '    Next

                'Catch ex As Exception

                'End Try
            End If

            Dim FeaturedProductUserControl As Control
            FeaturedProductUserControl = LoadControl(ControlTxt)

            Controls.Add(FeaturedProductUserControl)
            PlaceHolder1.Controls.Add(FeaturedProductUserControl)

            ViewState("usercontrol") = ControlTxt

            AddControls(ControlTxt)

            ' PlaceHolder1.UpdateAfterCallBack = True
        Catch ex As Exception

            ViewState("usercontrol") = ""
        End Try

    End Sub
    Sub AddControls(ByVal control As String)

        If ViewState("controles") Is Nothing Then
            ViewState("controles") = control
        Else
            ViewState("controles") = ViewState("controles") & ";" & control
        End If


    End Sub
    Sub RemoveControls()
        Try
            Dim i As Integer
            Dim j As Integer = PlaceHolder1.Controls.Count - 1

            For i = 0 To PlaceHolder1.Controls.Count - 1
                PlaceHolder1.Controls.Remove(PlaceHolder1.Controls(j))
                'PlaceHolder1.UpdateAfterCallBack = True
                j = j - 1
            Next

            For i = 0 To ViewState("controles").ToString.Split(";").Length - 1

                Try
                    Dim LastFeaturedProductUserControl As Control
                    LastFeaturedProductUserControl = LoadControl(ViewState("controles").ToString.Split(";")(i))
                    PlaceHolder1.Controls.Remove(LastFeaturedProductUserControl)
                    'PlaceHolder1.UpdateAfterCallBack = True

                Catch ex As Exception

                End Try


            Next

            ViewState("controles") = Nothing
        Catch ex As Exception

        End Try
    End Sub

    Protected Sub TreeView1_SelectedNodeChanged(ByVal sender As Object, ByVal e As EventArgs) Handles TreeView1.SelectedNodeChanged
        CargaControl(TreeView1.SelectedValue.Replace(".ascx", "") & ".ascx", True)
    End Sub
End Class